package com.ayoka.ui.bespoke;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import com.ayoka.R;
import com.ayoka.adapter.BespokeCoachTimeAdapter;
import com.ayoka.base.BaseRecyclerViewActivity;
import com.ayoka.databinding.ActivityBespokeCoachBinding;
import com.ayoka.mvp.BespokeCoachMVP.BespokeCoachModel.BespokeCoachBean;
import com.ayoka.mvp.BespokeCoachMVP.BespokeCoachPresenter;
import com.ayoka.mvp.BespokeCoachMVP.IBespokeCoachView;
import com.ayoka.ui.meditation.MeditationDetailActivity;
import com.ayoka.ui.operate_result.OperateResultActivity;
import com.ayoka.widget.CustomDayView;
import com.jcp.baselibrary.brvah.BaseQuickAdapter;
import com.jcp.baselibrary.brvah.BaseViewHolder;
import com.jcp.baselibrary.brvah.RecycleViewDivider;
import com.jcp.baselibrary.model.ToolBarOptions;
import com.jcp.baselibrary.utils.UIUtils;
import com.ldf.calendar.Utils;
import com.ldf.calendar.component.CalendarAttr;
import com.ldf.calendar.component.CalendarViewAdapter;
import com.ldf.calendar.interf.OnSelectDateListener;
import com.ldf.calendar.model.CalendarDate;
import com.ldf.calendar.view.Calendar;
import com.ldf.calendar.view.MonthPager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;


/**
 * @author jcp
 * @ClassName: TopicListActivity
 * @Description:
 * @date 2018-12-28 下午4:21:18
 */
public class BespokeCoachActivity extends BaseRecyclerViewActivity<String, ActivityBespokeCoachBinding, BespokeCoachPresenter> implements IBespokeCoachView {

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, BespokeCoachActivity.class);
        activity.startActivity(intent);
    }

    private ArrayList<Calendar> currentCalendars = new ArrayList<>();
    private CalendarViewAdapter calendarAdapter;

    private CalendarDate currentDate;

    @Override
    public int getContentLayout() {
        return R.layout.activity_bespoke_coach;
    }

    @Override
    protected void initViews() {
        super.initViews();
        //此处强行setViewHeight，毕竟你知道你的日历牌的高度
        mDataBinding.calendarView.setViewHeight(Utils.dpi2px(this, 270));

        initCalendarView();
    }

    @Override
    protected SwipeRefreshLayout getRefreshLayout() {
        return null;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        return mDataBinding.recyclerView;
    }

    @Override
    protected BaseQuickAdapter<String, BaseViewHolder> getAdapter() {
        BespokeCoachTimeAdapter adapter = new BespokeCoachTimeAdapter();
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String time = (String) adapter.getItem(position);
                mPresenter.showConfirmBespokeDialog(time);
            }
        });
        return adapter;
    }

    @Override
    protected RecyclerView.ItemDecoration getRecycleDivider() {
        return new RecycleViewDivider.Builder(this)
                .setStyle(RecycleViewDivider.Style.BETWEEN)
                .setColor(UIUtils.getColor(R.color.gray_dc))
                .setSize(TypedValue.COMPLEX_UNIT_PX, UIUtils.getDimensionPixelSize(R.dimen.dp_0_5))
                .build();
    }

    @Override
    protected void getData(boolean isShowWaitDialog) {

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions(R.string.answer_question_bespoke);
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        mPresenter.getTimeListData();

        currentDate = new CalendarDate();
        mPresenter.getBespokeCoachData(currentDate.toString());
    }

    @Override
    protected BespokeCoachPresenter getPresenter() {
        return new BespokeCoachPresenter();
    }


    @Override
    public void configBespokeCoachData(BespokeCoachBean bespokeCoachBean) {

    }

    @Override
    public Activity getActivityView() {
        return this;
    }

    @Override
    public CalendarDate getBespokeDate() {
        return currentDate;
    }

    @Override
    public void onBespokeSuccess(String date) {
        OperateResultActivity.start(this, OperateResultActivity.OPERATE_BESPOKE_COACH, "2019-01-26 15:15:15");
    }

    @Override
    public void configBespokeCoachTimeListData(List<String> timeListData) {
        setLoadMoreLayout(timeListData);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 以周模式启动，请在onResume调用
        Utils.scrollTo(mDataBinding.content, mDataBinding.recyclerView, mDataBinding.calendarView.getCellHeight(), 200);
        calendarAdapter.switchToWeek(mDataBinding.calendarView.getRowIndex());
        // 是否可以展开
        mDataBinding.calendarView.setDrawExpand(true);
    }

    /**
     * 初始化CustomDayView，并作为CalendarViewAdapter的参数传入
     */
    private void initCalendarView() {
        OnSelectDateListener onSelectDateListener = new OnSelectDateListener() {
            @Override
            public void onSelectDate(CalendarDate date) {
                currentDate = new CalendarDate();
            }

            @Override
            public void onSelectOtherMonth(int offset) {
                //偏移量 -1表示刷新成上一个月数据 ， 1表示刷新成下一个月数据
                mDataBinding.calendarView.selectOtherMonth(offset);
            }
        };
        CustomDayView customDayView = new CustomDayView(this, R.layout.custom_day);
        calendarAdapter = new CalendarViewAdapter(
                this,
                onSelectDateListener,
                CalendarAttr.WeekArrayType.Sunday,
                customDayView);
        calendarAdapter.setOnCalendarTypeChangedListener(new CalendarViewAdapter.OnCalendarTypeChanged() {
            @Override
            public void onCalendarTypeChanged(CalendarAttr.CalendarType type) {
                mDataBinding.recyclerView.scrollToPosition(0);
            }
        });
        initMarkData();
        initMonthPager();
    }

    /**
     * 初始化标记数据，HashMap的形式，可自定义
     * 如果存在异步的话，在使用setMarkData之后调用 calendarAdapter.notifyDataChanged();
     */
    private void initMarkData() {
        HashMap<String, String> markData = new HashMap<>();
        markData.put("2019-1-20", "1");
        markData.put("2019-1-21", "0");
        markData.put("2019-1-26", "1");
        calendarAdapter.setMarkData(markData);
    }

    /**
     * 初始化monthPager，MonthPager继承自ViewPager
     *
     * @return void
     */
    private void initMonthPager() {
        mDataBinding.calendarView.setAdapter(calendarAdapter);
        mDataBinding.calendarView.setCurrentItem(MonthPager.CURRENT_DAY_INDEX);
        mDataBinding.calendarView.setPageTransformer(false, new ViewPager.PageTransformer() {
            @Override
            public void transformPage(View page, float position) {
                position = (float) Math.sqrt(1 - Math.abs(position));
                page.setAlpha(position);
            }
        });
        mDataBinding.calendarView.addOnPageChangeListener(new MonthPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                Log.d("ldf", "onPageSelected " + position);
                currentCalendars = calendarAdapter.getPagers();
                if (currentCalendars.get(position % currentCalendars.size()) != null) {
                    CalendarDate date = currentCalendars.get(position % currentCalendars.size()).getSeedDate();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }
}
